<?php

namespace App\Http\Controllers\UserApi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\UserIntegral;
use App\Models\IntegralPlan;
use App\Http\Requests\UserApi\IntegralPlanRequest;
use App\Models\UserIntegralUsable;
use App\Models\UserIntegralChangeRecord;

class UserIntegralController extends BaseController
{

    public function apply_integral(IntegralPlanRequest $request, UserIntegral $integral, IntegralPlan $integralPlan)
    {
        $integralPlanObj = $integralPlan->where('id', $request->integral_plan_id)->first();
        $res = $integral->insert([
            'user_id' => auth('api')->user()->id,
            'integral' => $integralPlanObj->integarl,
            'proof_img' => $request->proof_img,
            'integral_plan_id' => $request->integral_plan_id,
            'pay_type_id' => $request->pay_type_id,
            'valid_total_time' => $integralPlanObj->valid_duration,
            'created_at' => date("Y-m-d H:i:s")
        ]);
        if ($res) {
            return $this->success($res, 200, '等待审核申请成功');
        }
        return $this->fail('失败');
    }

    public function my_integral(Request $request, UserIntegral $integral)
    {
        $member_id = auth('api')->user()->id;
        $query = $integral->where('user_id', $member_id);
        if (!empty($request->input('where_date'))) {
            $query->whereDate('created_at', $request->input('where_date'));
        }
        if (!empty($request->input('status'))) {
            $query->where('status', $request->input('status'));
        }
        $data = $query
            ->with([
                'user' => function ($q) {
                    $q->select([
                        'id',
                        'head_img',
                        'nickname',
                    ]);
                },
                'integral_plan' => function ($q) {
                    $q->select([
                        'id',
                        'title',
                        'content',
                        'integarl',
                    ]);
                },
                'pay_type' => function ($q) {
                    $q->select([
                        'id',
                        'pay_name',
                    ]);
                },
            ])
            ->orderBy('id','desc')->paginate(6);
        return $this->success($data);
    }

    public function integral_change_record(Request $request,UserIntegralChangeRecord $integralChangeRecord){
        $query = $integralChangeRecord->where('user_id',auth('api')->user()->id);
        if (!empty($request->input('where_date'))) {
            $start=date('Y-m-01 00:00:00',strtotime($request->input('where_date')));//获取指定月份的第一天
            $end=date('Y-m-t 23:59:59',strtotime($request->input('where_date'))); //获取指定月份的最后一天

            $query->whereBetween('created_at',[$start,$end]);
        }
        if (!empty($request->input('status'))) {
            $query->where('status', $request->input('status'));
        }
        $data = $query
            ->select([
                'integral',
                'chage_type',
                'expire_time',
                'reason',
                'created_at',
            ])
            ->orderBy('id','desc')
            ->paginate(10);
        return $this->success($data);
    }
}

